package com.itqf.realm;

import com.itqf.entity.SysUsers;
import com.itqf.service.UserService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/10/14
 * @Time: 上午11:08
 */
public class MyUserRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("------->授权");
        //用户对象是在认证通过后，使用SimpleAuthenticationInfo 存储的
        //this.principals = new SimplePrincipalCollection(principal, realmName);
        SysUsers  sysUsers =  (SysUsers) principals.getPrimaryPrincipal();
       //查==>查询用户的角色----> 权限

//        List roles = new ArrayList();//角色
//        roles.add("admin");
//        roles.add("ceo");
//
//        List perms = new ArrayList();//权限
//        perms.add("sys:product:*");// sys:product:add  sys:product:update  sys:product:delete
//        perms.add("sys:user:*");// sys:product:add  sys:product:update  sys:product:delete
//        perms.add("sys:menu:add");// sys:product:add  sys:product:update  sys:product:delete

        //根据用户编号查==>查询用户的角色---->然后再查询权限
        List<String> roles = userService.findRolesByUid(sysUsers.getId());
        List<String> perms = userService.findPermsByUid(sysUsers.getId());


        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.addRoles(roles);//角色集合
        info.addStringPermissions(perms);//权限集合

        return info;
    }

    /**
     *  subject.login(token);//查找realm  执行认证方法
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("------->认证 登录");
        // public Object getPrincipal() {
        //        return getUsername();
        //    }
        String username  = (String) token.getPrincipal();
        // public char[] getPassword() {
        //        return password;
        //    }
        String password = new String((char[])token.getCredentials());

        //比较逻辑   注入service     调用登录逻辑
//        String  u = "admin";//数据库中存储的用户名
//       // String p = "123";
//        String p = "c41d7c66e1b8404545aa3a0ece2006ac";//代表数据库存储的密文
//
//        int  isLock = 1;//状态
        //User  user;
        SysUsers sysUsers = userService.findByUsername(username);


        if (sysUsers==null){//账号错误
            throw  new UnknownAccountException("账号不存在");
        }
        if (!password.equals(sysUsers.getPassword())){
            throw new IncorrectCredentialsException("密码错误");
        }
        if (sysUsers.getStatus()!=1){
            throw new LockedAccountException("账户锁定");
        }


        SimpleAuthenticationInfo info = new
                SimpleAuthenticationInfo(sysUsers,password,this.getName());

        return info;
    }
}
